package com.doctor.windflower_doctor.activity;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;

public class DoctorAuth extends BaseActivity {
	private ImageButton back_imageButton;
	private Button submit_Button, auth_Button, upload_Button,
			modification_Button, cancel_button;
	private EditText name_EditText, phone_EditText, auth_EditText,
			email_EditText, jobTitle_EditText, practice_EditText;
	private RadioGroup radioGroup;
	private ImageView photo_ImageView, album, take_photo, delete_imageView;
	private RelativeLayout administrativeoffice_relativelayout,
			job_relativelayout, big_layout, black_backgroungd,
			image_background;
	private TextView administrativeoffice_textView, job_textView;
	private Dialog dialog;
	private PopupWindow popWindow;
	private View photo_view;
	private String imageFilePath, sex = "男";
	private int CAMERA_RESULT = 5;
	private int PHOTO_RESULT = 1;
	private File imageFile;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath);
		photo_ImageView.setImageBitmap(bitmap);

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		cancel_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popWindow.dismiss();
			}
		});
		black_backgroungd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popWindow.dismiss();
			}
		});
		upload_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popWindow.showAtLocation(big_layout, Gravity.CENTER, 0, 0);
			}
		});
		back_imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		submit_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(DoctorAuth.this, Audit.class));
			}
		});
		take_photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Uri imageFileUri = Uri.fromFile(imageFile);
				Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				i.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
				startActivityForResult(i, CAMERA_RESULT);

			}
		});
		album.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 调用android的图库
				startActivityForResult(i, PHOTO_RESULT);

			}
		});
		modification_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popWindow.showAtLocation(big_layout, Gravity.CENTER, 0, 0);
			}
		});
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (checkedId == R.id.man) {
					sex = "男";
				} else {
					sex = "女";
				}
			}
		});
		photo_ImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(DoctorAuth.this,
						ShowPhotoActivity.class));
			}
		});
		delete_imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				phone_EditText.setText("");
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			imageFilePath = Environment.getExternalStorageDirectory()
					.getAbsolutePath() + "/mypicture.png";
		} else {
			imageFilePath = getFilesDir().getAbsolutePath() + "/mypicture.png";
		}

		imageFile = new File(imageFilePath);
		delete_imageView = (ImageView) findViewById(R.id.delete_imageView);
		image_background = (RelativeLayout) findViewById(R.id.bk);
		big_layout = (RelativeLayout) findViewById(R.id.docotorauth_layout);
		photo_view = LayoutInflater.from(DoctorAuth.this).inflate(
				R.layout.takephoto_pop, null);
		back_imageButton = (ImageButton) findViewById(R.id.back_imageButton);
		submit_Button = (Button) findViewById(R.id.submit_button);
		auth_Button = (Button) findViewById(R.id.anth_button);
		upload_Button = (Button) findViewById(R.id.uplaod_button);
		modification_Button = (Button) findViewById(R.id.modification_button);
		name_EditText = (EditText) findViewById(R.id.name_editText);
		phone_EditText = (EditText) findViewById(R.id.phone_editText);
		auth_EditText = (EditText) findViewById(R.id.auth_editText);
		email_EditText = (EditText) findViewById(R.id.email_editText);
		jobTitle_EditText = (EditText) findViewById(R.id.jobtitle_editText);
		practice_EditText = (EditText) findViewById(R.id.number_editText);
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
		photo_ImageView = (ImageView) findViewById(R.id.photo_imageview);
		administrativeoffice_relativelayout = (RelativeLayout) findViewById(R.id.administrativeoffice_relativelayout);
		job_relativelayout = (RelativeLayout) findViewById(R.id.job_relativelayout);
		administrativeoffice_textView = (TextView) findViewById(R.id.administrativeoffice_textView);
		job_textView = (TextView) findViewById(R.id.job_textView);
		dialog = new Dialog(DoctorAuth.this);
		popWindow = new PopupWindow(photo_view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		album = (ImageView) photo_view.findViewById(R.id.album_imageView);
		take_photo = (ImageView) photo_view
				.findViewById(R.id.takephoto_imageView);
		black_backgroungd = (RelativeLayout) photo_view
				.findViewById(R.id.black_backgroungd);
		cancel_button = (Button) photo_view.findViewById(R.id.cancel_button);

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == CAMERA_RESULT && arg1 == RESULT_OK) {

			BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
			bmpFactoryOptions.inJustDecodeBounds = true;
			Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,
					bmpFactoryOptions);
			bmpFactoryOptions.inSampleSize = 10;
			bmpFactoryOptions.inJustDecodeBounds = false;
			bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
			photo_ImageView.setImageBitmap(bmp);
			modification_Button.setVisibility(View.VISIBLE);
			image_background.setVisibility(View.VISIBLE);
			upload_Button.setVisibility(View.GONE);
			popWindow.dismiss();
			BufferedOutputStream bos;
			try {
				bos = new BufferedOutputStream(new FileOutputStream(imageFile));
				bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
				bos.flush();
				bos.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else if (arg0 == PHOTO_RESULT && arg1 == RESULT_OK) {

			Uri uri = arg2.getData();
			Cursor cursor = DoctorAuth.this.getContentResolver().query(uri,
					null, null, null, null);
			cursor.moveToFirst();
			String imgPath = cursor.getString(1); // 图片文件路径
			cursor.close();
			Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = false;
			options.inSampleSize = 5;
			Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
			photo_ImageView.setImageBitmap(bitmap);
			modification_Button.setVisibility(View.VISIBLE);
			image_background.setVisibility(View.VISIBLE);
			upload_Button.setVisibility(View.GONE);
			popWindow.dismiss();
			BufferedOutputStream bos;
			try {
				bos = new BufferedOutputStream(new FileOutputStream(imageFile));
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
				bos.flush();
				bos.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.doctor_auth;
	}

}
